<?php
namespace components\orm\dbclass;
class sqldb {
    public $config;
    public $link;
    public $order;
    public $limit;
    public $column = "*";
    public $condition;
    public $record = array();
    public function __construct($config){
        $this->config = $config;
        $dsn = "";
        if($config['driver'] == "sqlite"){
            $dsn = "sqlite:".$config['dbname'];
            $this->link = new \PDO($dsn);
        }else{
            $dsn = $config['driver'].": host=".$config['host'].';dbname='.$config['dbname'];
            $this->link = new \PDO($dsn,  $config['user'],  $config['pass'],array(PDO_ATTR_PERSISTENT => true));
        }
        $this->link->query("set names ".$config['charset']);
    }
    public function prepare($record){
        $this->record = $record;
    }
    public function create($table){
        $record = $this->record;
        $rk = array();
        $rv = array();
        foreach($record as $key => $value){
            $rk[] = $key;
            $rv[] = "'$value'";
        }
        $sql = "insert into ".$table."(".implode(",",$rk).") values(".implode(",",$rv).");";
        $this->link->beginTransaction();
        $result = $this->link->exec($sql);
        $this->link->commit();
        $this->condition = "";
        return $result;
    }
    public function readOne($table){
        $sql = "select ".$this->column." from ".$table.$this->condition;
        $rs = $this->link->prepare($sql);
        $rs->execute();
        $result = $rs->fetch();
        $this->condition = "";
        return $result;
    }
    public function read($table){
        $sql = "select ".$this->column." from ".$table.$this->limit.$this->order.$this->condition;
        $rs = $this->link->prepare($sql);
        $rs->execute();
        $result = $rs->fetchAll();
        $this->column = "*";
        $this->condition = "";
        return $result;
    }
    public function update($table){
        $record = $this->record;
        $sql = "update ".$table." set ";
        foreach($record as $key=>$value){
            $sql .= " ".$key."="."'$value',";
        }
        $sql = trim($sql,",").$this->condition;
        $this->link->beginTransaction();
        $result = $this->link->exec($sql);
        $this->link->commit();
        $this->condition = "";
        return $result;
    }
    public function delete($table){
        $this->link->beginTransaction();
        $result = $this->link->exec($sql);
        $this->link->commit();
        $this->condition = "";
        return $result;
    }
    public function setCondition($condition){
        if(is_array($condition) && !empty($condition)){
            foreach ($condition as $key => $value) {
                $this->condition .= $key."="."'$value' AND ";
            }
            $this->condition = " where ".trim($this->condition, " AND ");
        }
    }
    public function setColumn($clomun){
        if(is_array($clomun) && !empty($clomun)){
            $this->column = "";
            foreach($clomun as $value){
                $this->column .= $value.",";
            }
            $this->column = trim($this->column,",");
        }
    }
    public function getColumn($table){
        $result = $this->readOne($table);
        foreach($result as $key => $value){
            $column[] = $key;
        }
        return $column;
    }
    public function setLimit($start = "",$stop = ""){
          if(is_int($start) && is_int($stop)){
              $this->limit = " limit ".$start.",".$stop." ";
          }else{
              $this->limit = "";
          }
    }
    public function setOrder($field,$type = "desc"){
        if($field != ""){
            if ($type == "asc") {
                $this->order = " order by ".$field." asc ";
            }else{
                $this->order = " order by".$field."desc ";
            }
        }else{
            $this->order = "";
        }
    }
}

?>
